Collection Manipulation Functions: conj, assoc, dissoc

Computer Programming - ক্লোজার (Clojure) Collections এবং Sequence (Collections and Sequences) |
266
266

Collection Manipulation Functions in Clojure: conj, assoc, dissoc

Clojure provides several powerful functions to manipulate collections such as lists, vectors, maps, and sets. Three of the most commonly used functions for modifying collections are conj, assoc, and dissoc. These functions are all designed to work in an immutable way, meaning they don't modify the original collection but instead return a new modified collection.

Let's go over each function with examples:


1. conj (Add to collection)

The conj function is used to add elements to a collection. The behavior of conj depends on the type of collection it is applied to:

  • For a list, it adds the element to the front of the list (since lists are linked structures).
  • For a vector, it adds the element to the end of the vector.
  • For a set, it adds the element to the set, ensuring that there are no duplicates.

Example: conj with Lists, Vectors, and Sets

; Using conj with a List
(def my-list '(2 3 4))
(def new-list (conj my-list 1)) 
; my-list is unchanged, new-list is (1 2 3 4)
(println new-list)

; Using conj with a Vector
(def my-vector [1 2 3])
(def new-vector (conj my-vector 4))
; my-vector is unchanged, new-vector is [1 2 3 4]
(println new-vector)

; Using conj with a Set
(def my-set #{2 3})
(def new-set (conj my-set 1))
; my-set is unchanged, new-set is #{1 2 3}
(println new-set)
  • In the case of the list, 1 is added at the front.
  • For the vector, 4 is added at the end.
  • In the set example, the number 1 is added to the set if it’s not already present.

2. assoc (Add or update key-value pairs in a map)

The assoc function is used to add or update key-value pairs in a map. If the key already exists, it updates the value associated with that key. If the key does not exist, it adds the key-value pair to the map.

Example: assoc with Maps

; Using assoc to add or update key-value pairs in a Map
(def my-map {:name "Alice" :age 30})
(def new-map (assoc my-map :age 31 :city "New York"))
; my-map is unchanged, new-map is {:name "Alice", :age 31, :city "New York"}
(println new-map)
  • The :age key is updated with the new value 31.
  • The :city key-value pair is added to the map.

3. dissoc (Remove key-value pairs from a map)

The dissoc function removes one or more key-value pairs from a map. It returns a new map with the specified keys removed.

Example: dissoc with Maps

; Using dissoc to remove key-value pairs from a Map
(def my-map {:name "Alice" :age 30 :city "New York"})
(def new-map (dissoc my-map :age :city))
; my-map is unchanged, new-map is {:name "Alice"}
(println new-map)
  • The keys :age and :city are removed from the map.

Summary of conj, assoc, and dissoc

FunctionDescriptionExample Usage
conjAdds an element to a collection (front for lists, end for vectors, and to the set)(conj [1 2] 3)[1 2 3], (conj #{1 2} 3)#{1 2 3}
assocAdds or updates key-value pairs in a map(assoc {:a 1} :b 2){:a 1 :b 2}
dissocRemoves one or more key-value pairs from a map(dissoc {:a 1 :b 2} :b){:a 1}

Key Points to Remember:

  • conj modifies the collection by adding elements at the correct position based on the type (beginning for lists, end for vectors).
  • assoc is specifically used to add or modify key-value pairs in a map.
  • dissoc removes key-value pairs from a map.

These functions return a new collection and do not mutate the original collection, which is a core principle of functional programming and immutability in Clojure.

common.content_added_by
টপ রেটেড অ্যাপ

স্যাট অ্যাকাডেমী অ্যাপ

আমাদের অল-ইন-ওয়ান মোবাইল অ্যাপের মাধ্যমে সীমাহীন শেখার সুযোগ উপভোগ করুন।

ভিডিও
লাইভ ক্লাস
এক্সাম
ডাউনলোড করুন
Promotion